const log = require('../util/log');

module.exports = (req, res) => {
  require('../util/service').handlePost(req, res, (reqObj, resolve) => {
    if(!reqObj.url) {
      resolve({
        result : -1,
        msg : '缺少url'
      })
      return
    }
    require('../dao/urlDao').select({
      url: reqObj.url
    }, urls => {
      if(urls.length == 0) {
        resolve({
          result: -1,
          msg: '找不到url'
        })
        return
      }
      if(urls.length > 1) {
        resolve({
          result: -1,
          msg: '找到多个url'
        })
        return
      }
      urls[0].getCaches(caches => {
        caches.forEach(cache => {
          delete cache.html
        })
        resolve({
          result: 1,
          caches: caches
        })
      })
    })
  })
}
